#!/bin/bash

#####################################
# Install penguins-eggs-tarball
function install_tarball() {
    EGGS_PACKAGE=$1
    DISTRO=$2

    echo ""
    echo "install package penguins-eggs on ${DISTRO} from ${EGGS_PACKAGE}"
    echo ""

    # Controllo se il pacchetto tarball esiste
    if [ ! -f "$EGGS_PACKAGE" ]; then
         echo "Error: file $EGGS_PACKAGE don't exists."
        exit 1
    fi

    EGGS_HOME="/opt/penguins-eggs/"

    # Rimozione di /opt/penguins-eggs se esiste
    if [ -d "$EGGS_HOME" ]; then
        $SUDO rm -rf "$EGGS_HOME"
    fi

    # Estrazione e installazione del pacchetto
    TEMP_DIR=$(mktemp -d -p "/tmp" "eggs-unpacked-XXXXXX")
    tar -xzf "$EGGS_PACKAGE" -C "$TEMP_DIR"
    # tar -xzf "$EGGS_PACKAGE"
    if [ $? -ne 0 ]; then
        echo "Error: not possoble extract $EGGS_PACKAGE."
        exit 1
    fi

    mv $TEMP_DIR/eggs $TEMP_DIR/penguins-eggs
    $SUDO mv $TEMP_DIR/penguins-eggs /opt/

    # Ricreazione dei link per i temi grub/isolinux
    $SUDO ln -sf "${EGGS_HOME}addons/eggs/theme/livecd/isolinux.main.full.cfg" "${EGGS_HOME}addons/eggs/theme/livecd/isolinux.main.cfg"
    $SUDO ln -sf "${EGGS_HOME}addons/eggs/theme/livecd/grub.main.full.cfg" "${EGGS_HOME}addons/eggs/theme/livecd/grub.main.cfg"

    # Bash completions
    if [ -d "/usr/share/bash-completion/completions/" ]; then
        $SUDO rm -f /usr/share/bash-completion/completions/eggs.bash
        $SUDO ln -sf "${EGGS_HOME}scripts/eggs.bash" /usr/share/bash-completion/completions/eggs.bash
    fi

    # Zsh completions
    if [ -d "/usr/share/zsh/functions/Completion/Zsh/" ]; then
        $SUDO rm -f /usr/share/zsh/functions/Completion/Zsh/_eggs
        $SUDO ln -sf "${EGGS_HOME}scripts/_eggs" /usr/share/zsh/functions/Completion/Zsh/
    fi

    # Icone
    if [ -d "/usr/share/icons/" ]; then
        $SUDO rm -f /usr/share/icons/eggs.png
        $SUDO ln -sf "${EGGS_HOME}assets/eggs.png" /usr/share/icons/eggs.png
    fi

    # Manuali
    if [ -d "/usr/share/man/man1" ]; then
        $SUDO rm -f /usr/share/man/man1/eggs.1.gz
        $SUDO ln -sf "${EGGS_HOME}manpages/doc/man/eggs.1.gz" /usr/share/man/man1/eggs.1.gz
    fi

    # Link binario
    $SUDO rm -f /usr/bin/eggs
    $SUDO ln -sf "${EGGS_HOME}bin/eggs" /usr/bin/eggs

    # Collegamenti desktop
    xdg-user-dirs-update --force
    DESKTOP_DIR=$(xdg-user-dir DESKTOP)
    cp "${EGGS_HOME}addons/eggs/adapt/applications/eggs-adapt.desktop" "${DESKTOP_DIR}"
    cp "${EGGS_HOME}assets/penguins-eggs.desktop" "${DESKTOP_DIR}"
    for f in "$DESKTOP_DIR"/*.desktop; do
        chmod +x "$f"
        gio set -t string "$f" metadata::xfce-exe-checksum "$(sha256sum "$f" | awk '{print $1}')" 2>&1 > /dev/null
    done

    # Configurazione di base
    $SUDO eggs config -n

    echo ""
    echo "tarballs $EGGS_PACKAGE was installed on $EGGS_HOME."
    exit 0
}

#####################################
# Install Requirements of the distro
function install_requirements() {

    echo "Requirements installations for distro: ${DISTRO}"
    echo ""
   
    case $DISTRO in
        aldos)
            $SUDO ./requirements/aldos/install.sh
            ;;

        almalinux|rocky)
            $SUDO ./requirements/rocky/install.sh
            ;;

        arch)
            $SUDO ./requirements/arch/install.sh
            ;;
        debian)
            $SUDO ./requirements/debian/install.sh
            ;;
        devuan)
            $SUDO ./requirements/devuan/install.sh
            ;;
        fedora)
            $SUDO ./requirements/fedora/install.sh
            ;;

        manjaro|biglinux)
            $SUDO ./requirements/manjarolinux/install.sh
            ;;

        nobara)
            $SUDO ./requirements/nobara/install.sh
            ;;

        openmamba)
            $SUDO ./requirements/openmamba/install.sh
            ;;

        opensuse-tumbleweed)
            $SUDO ./requirements/opensuse/install.sh
            ;;

        ubuntu|linuxmint)
            $SUDO ./requirements/ubuntu/install.sh
            ;;

        *)
            echo "This distro: $DISTRO is not supported!"
            exit 1
            ;;
    esac

    echo ""
    echo "Requirements  of $EGGS_PACKAGE was installed."
}

#
# main
#

clear
echo "installing penguins-eggs-tarball..."
echo ""

# Se l'utente è root, abortisci
if [ "$(id -u)" -eq 0 ]; then
    echo "dont'use root!"
    echo "usage: setup /path/to/tarball [requirements]"
    exit 1
fi

# Controllo dei parametri
if [ -z "$1" ]; then
    echo "Uso: setup /path/to/penguins-eggs-tarball [requisites]"
    exit 1
fi

# Rilevamento della distribuzione Linux
if [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO=$ID
#elif [ -f /etc/redhat-release ]; then
#    DISTRO=$(awk '{print $1}' /etc/redhat-release)
else
    echo "Distro not recognized."
    exit 1
fi
echo "Distro recognized: ${DISTRO}"

# Determinazione di SUDO
if command -v sudo >/dev/null 2>&1; then
    SUDO='sudo'
elif command -v doas >/dev/null 2>&1; then
    SUDO='doas'
else
    echo "Errore: né sudo né doas sono disponibili."
    exit 1
fi

# Installazione dei requisiti se il secondo parametro è specificato
if [ -n "$2" ]; then
    install_requirements $DISTRO
fi

# Installazione del pacchetto tarball
install_tarball $1 $DISTRO
